import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {List, Detail} from './DTO';
const baseUrl = '/afterSales/api/v1';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: 'SOC跟踪'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '立项{resourceName}'
}, {resourceName});


const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const joinDesc = formatMessage({
    id: 'api.message.joinDesc',
    defaultMessage: '加入项目{resourceName}'
}, {resourceName});

const closeDesc = formatMessage({
    id: 'api.message.closeDesc',
    defaultMessage: '关闭{resourceName}'
}, {resourceName});

const getSocsDesc = formatMessage({
    id: 'api.message.resettleDesc',
    defaultMessage: '查询SOC'
});
const initQueryDesc = formatMessage({
    id: 'api.message.initQueryDesc',
    defaultMessage: '初始化'
});

// 初始化查询
export const initQuery = () => request(`${baseUrl}/brands`, null, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, initQueryDesc));

// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/socClaims?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};
// 查询详情
export const getDetail = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/socClaims/details?${queryString}`, null, Detail)
        .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));
};
// 立项
export const addInfo = data => request(`${baseUrl}/socs/forSocClaim`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 加入项目
export const joinInfo = data => request(`${baseUrl}/socs/forSocClaim/join`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, joinDesc));

// 关闭
export const closeInfo = data => request(`${baseUrl}/socClaims/close`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, closeDesc));

// 查询SOC
export const getSocs = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/socs?${queryString}`, null, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getSocsDesc));
};
